import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../res.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/Tool/MyTools.dart';
import 'package:get/get.dart';
import '../../api/ApiUtils.dart';
import '../../api/request/LoginRegisterRequest.dart';
import '../../api/request/UserInfoRequest.dart';
import '../../base/MyScaffold.dart';
import '../../config/Constant.dart';
import '../../utils/color/ColorUtils.dart';
import '../../utils/userbloc/user_bloc.dart';
import '../../widgets/MyImage.dart';
import '../../widgets/TextView.dart';
import 'AccountChangePassword.dart';
import 'ChangePassword.dart';

class Bind extends StatefulWidget {
  String url;
  String name;
  String email;
  String token;

  Bind(
      {Key? key,
      required this.url,
      required this.name,
      required this.email,
      required this.token})
      : super(key: key);

  @override
  State<Bind> createState() => _BindState();
}

class _BindState extends State<Bind> {
  String? password;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  login_() {
    if (MyTools.stringIsEmpty(password)) {
      MyToast().show("Please enter your password for verification",
          icon_tips: false);
      return;
    }
    apiShow(
        api: login(email: widget.email, password: generateMD5(password!)),
        success: (result) async {
          await UserBlocTool.saveAuth(result.data[Constant.Authorization]);
          Info();
        },
        failure: (e) {
          MyToast().show(e.msg, icon_tips: false);
        });
  }

  googleBind() {
    apiShow(
        api: google(widget.token),
        success: (result) {
          // UserBlocTool.saveAuth(result.data[Constant.Authorization]);
          MyToast().show("Bind Successful");
        },
        failure: (e) {
          MyToast().show(e.msg, icon_tips: false);
        });
  }

  void Info() {
    if (!MyTools.stringIsEmpty(UserBlocTool.userAuth)) {
      apiShow(
          api: info(),
          success: (result) {
            print(UserBlocTool.getUser()!.toJson());
            googleBind();
          },
          failure: (e) {
            // MyToast().show(e.msg);
          });
    }
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController phoneContro = TextEditingController();
    FocusNode phoneFocus = FocusNode();
    return MyScaffold(
      bottomline: false,
      body: Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 32),
        child: Column(
          children: [
            TextView(
              "Bind to your PDF Editor account",
              maxLines: 2,
              alignment: Alignment.centerLeft,
              style: TextStyle(
                  color: rgba(48, 49, 51, 1),
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            TextView(
              "We found a PDF editor account associated with your email. Do you want to link your Google account to PDF Editor?",
              style: TextStyle(color: rgba(100, 101, 102, 1)),
              maxLines: 10,
              margin: const EdgeInsets.symmetric(vertical: 8),
            ),
            Container(
              height: 98,
              padding: const EdgeInsets.all(20),
              margin: const EdgeInsets.only(top: 40),
              decoration: BoxDecoration(
                  color: rgba(247, 247, 247, 1),
                  borderRadius: BorderRadius.circular(6)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  MyImage(
                    widget.url,
                    width: 58,
                    height: 58,
                    radius: 8,
                    margin: EdgeInsets.only(right: 10),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      TextView(
                        widget.name,
                        style: TextStyle(
                            color: ColorUtils.text,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                      TextView(
                        widget.email,
                        margin: EdgeInsets.only(top: 6),
                        style: TextStyle(color: ColorUtils.text_),
                      )
                    ],
                  )
                ],
              ),
            ),
            TextView(
              "Password for existing PDF editor account",
              alignment: Alignment.centerLeft,
              margin: const EdgeInsets.only(top: 24),
              style: TextStyle(color: rgba(100, 101, 102, 1)),
              maxLines: 10,
            ),
            Container(
              margin: const EdgeInsets.only(top: 12),
              padding: const EdgeInsets.symmetric(horizontal: 12),
              alignment: Alignment.center,
              height: 48,
              decoration: BoxDecoration(
                  border: Border.all(
                    color: rgba(230, 231, 235, 1),
                    width: 1,
                  ),
                  borderRadius: const BorderRadius.all(Radius.circular(10))),
              child: TextField(
                focusNode: phoneFocus,
                keyboardType: TextInputType.phone,
                inputFormatters: [LengthLimitingTextInputFormatter(11)],
                controller: phoneContro,
                maxLines: 1,
                autofocus: false,
                style: const TextStyle(fontSize: 14),
                decoration: InputDecoration.collapsed(
                  hintText: 'password',
                  hintStyle: TextStyle(color: rgba(200, 202, 204, 1)),
                ),
                onChanged: (pass) {
                  // chechLoginClick();
                  //
                  password = pass;
                  print(password);
                  // setState(() {});
                },
              ),
            ),
            const SizedBox(
              height: 12,
            ),
            const SizedBox(
              height: 24,
            ),
            MaterialButton(
              onPressed: () {
                Get.to(const AccountChangePassword());
              },
              minWidth: double.infinity,
              color: rgba(255, 78, 77, 1),
              height: 48,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6),
              ),
              child: const TextView(
                "Continue",
                style: TextStyle(color: Colors.white),
              ),
            ),
            TextView(
              "Forget the password",
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.only(top: 24),
              style: TextStyle(color: rgba(23, 70, 143, 1)),
              onclick: () {
                Get.to(const ChangePassword());
              },
            )
          ],
        ),
      ),
    );
  }
}
